package com.lagou.controller;

import com.lagou.pojo.ResponseResult;
import com.lagou.util.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/demo")
public class DemoController {

    /**
     * url: http://localhost:8080/demo/handle01
     */
    @RequestMapping("/handle01")
    public ModelAndView handle01() {
        Date date = new Date();// 服务器时间
        // 返回服务器时间到前端页面
        // 封装了数据和页面信息的 ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        // addObject 其实是向请求域中request.setAttribute("date",date);
        modelAndView.addObject("date",date);
        // 视图信息(封装跳转的页面信息) 逻辑视图名
        modelAndView.setViewName("success");
        return modelAndView;
    }

    /**
     * 文件上传
     * @return
     */
    @RequestMapping(value = "/upload")
    public @ResponseBody void upload(MultipartFile uploadFile, HttpSession session, HttpServletResponse response, @RequestParam("name") String name) throws Exception {
        // 处理上传文件（重命名）
        String originalFilename = uploadFile.getOriginalFilename();// 原始名称
        String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1, originalFilename.length());// 扩展名  jpg
        String newName = UUID.randomUUID().toString() + "." + ext;

        // 存储,要存储到指定的文件夹，/uploads/yyyy-MM-dd，考虑文件过多的情况按照日期，生成一个子文件夹
//        String realPath = session.getServletContext().getRealPath("/uploads");//项目的webapp目录
        String realPath = session.getServletContext().getRealPath("/")+"../imageTest";//tomcat的webapp目录
        String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        File folder = new File(realPath + "/" + datePath);
        if(!folder.exists()) {
            folder.mkdirs();
        }
        // 存储文件到目录
        uploadFile.transferTo(new File(folder, newName));

        // TODO 文件磁盘路径要更新到数据库字段

        //返回
        ResponseResult result = new ResponseResult();
        result.setCode(0);
        result.setMessage("图片上传成功");

        ResultUtil.writeJson(response, result);

//        return "ok";
    }

    /**
     * url：http://localhost:8081/bbb/demo/handle02/1?name=xx
     */
    @RequestMapping(value = "/handle02/{id}",method = {RequestMethod.GET})
    public ModelAndView handleGet(@PathVariable("id") Integer id, HttpServletRequest request) {

        StringBuffer requestURL = request.getRequestURL();
        System.out.println("requestURL:" + requestURL);
        String requestURI = request.getRequestURI();
        System.out.println("requestURI:" + requestURI);

        String name = request.getParameter("name");
        System.out.println("name:" + name);

        Date date = new Date();
        ModelAndView modelAndView = new ModelAndView();
//        modelAndView.addObject("date",date);
        modelAndView.setViewName("success");
        return modelAndView;
    }

}
